#! /usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (c) Huoty, All rights reserved
# Author: Huoty <sudohuoty@gmail.com>
# CreateTime: 2019-04-14 20:48:12

import sys
import collections
import psutil


def main():
    pid_set = {int(sys.argv[1]) if len(sys.argv) > 1 else 1}
    tree = collections.defaultdict(list)
    for proc in psutil.process_iter():
        ppid = proc.ppid()
        if ppid not in pid_set:
            continue

        try:
            tree[ppid].append(proc.pid)
        except (psutil.NoSuchProcess, psutil.ZombieProcess):
            continue
        pid_set.add(proc.pid)

    for key, val in tree.items():
        print("{}: {}".format(key, val))



if __name__ == "__main__":
    main()
